package com.axend.aerosense.pay.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.utils.LogUtils;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.pay.viewmodel.VipHomeViewModel;
import com.axend.aerosense.pay.R;
import com.axend.aerosense.pay.databinding.PayActivityHomeBinding;
import com.axend.aerosense.pay.ui.fragment.PayVipHomeFragment;


/**
 * @author ChenHongWei
 * @date :2020/12/18 15:02
 */

@Route(path = RouterActivityPath.Pay.PAGE_PAY_HOME)
public class PayHomeActivity extends MvvmBaseActivity<PayActivityHomeBinding, BaseViewModel> implements ITitle {

    PayVipHomeFragment fragment = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        //设置系统状态栏为透明
//        getWindow().setStatusBarColor(Color.TRANSPARENT);
//        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        fragment = PayVipHomeFragment.getInstance();
        FragmentUtil.add(this,
                getContentId(),
                fragment,
                null,
                false);

        setSupportActionBar(viewDataBinding.payCustomToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportFragmentManager().addOnBackStackChangedListener(getListener());
    }

    public int getContentId() {
        return viewDataBinding.payContent.getId();
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.pay_activity_home;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    public Toolbar getToolBar() {
        return viewDataBinding.payCustomToolbar;
    }

    @Override
    public void setTitle(String title) {
        viewDataBinding.payTitle.setText(title);
    }

    private FragmentManager.OnBackStackChangedListener getListener() {
        FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                FragmentManager manager = getSupportFragmentManager();
                if (manager != null) {
                    Fragment currFrag = (Fragment) manager.findFragmentById(getContentId());
                    currFrag.onResume();
                }
            }
        };

        return result;
    }

    public void setTitleColor(int color) {
        viewDataBinding.payTitle.setTextColor(color);
    }

    public void setTitleBackground(int color) {
        viewDataBinding.payCustomToolbar.setBackgroundColor(color);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        LogUtils.d("myTAG", "PayHomeActivity-onActivityResult: ");
        if (requestCode == VipHomeViewModel.ZFB_PAY_AUTO_REQUEST_CODE_ID) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            finish();
            return;
        }
        super.onBackPressed();
    }

}
